﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Library_dll;
using Library_dll.Entities;
using System.Xml.Serialization;


namespace mis
{
    public partial class Mis : Form
    {
        Radar MissileComm = new Radar();
        Missile MisInfo = new Missile();
        double OurInterceptMis = (2000 * 1.46), CurrentDist;
        int MissileInventory = 25;
        int HitOrMiss;
        public Mis()
        {
            InitializeComponent();
            TmrQuery.Enabled = true;
            
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopMost = false;
        }

        private void alwaysOnTopToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TopMost = true;
        }

        private void Mis_Load(object sender, EventArgs e)
        {

        }

        private void TmrQuery_Tick(object sender, EventArgs e)
        {
            MisInfo.MissileONLINE1 = 1;
            MisInfo.InventoryTotal1 = MissileInventory;
            try
            {
                XmlSerializer serializer1 = new XmlSerializer(typeof(Missile));
                TextWriter textWriter1 = new StreamWriter("../../../../../../MisStatus.xml");
                serializer1.Serialize(textWriter1, MisInfo);
                textWriter1.Close();
            }
            catch (Exception ex)
            {
                //meh
            }
            try
            {
                XmlReader reader = new XmlTextReader("../../../../../../Comm_Serial.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                MissileComm = (Radar)serializer.Deserialize(reader);
                TmrUpdateDisplay.Enabled = true;
                reader.Close();
                CurrentDist = ((MissileComm.Distance * 5280) / 100);
                HitOrMiss = MissileComm.HitorMiss1;
            }
            catch (Exception ex)
            {
                //Write ex to Log
            }

           
            if (MissileComm.Firing_Status == 1)
            {

                MissileFired(MissileComm.Firing_Status);
            }
            Utility.DeleteFile("../../../../../../Comm_Serial.xml");
        }
        public void MissileFired(int MisFired)
        {
            int MisHitMiss = Utility.RandomNumber(0, 1);

        }

        private void TmrUpdateDisplay_Tick(object sender, EventArgs e)
        {
            if (CurrentDist > 0)
            {
                label2.Text = Convert.ToString(HitOrMiss);
                double HostMissSpeed = MissileComm.Speed * 1.46;
                CurrentDist = CurrentDist - (HostMissSpeed + OurInterceptMis);
                double currentPos = (CurrentDist - (HostMissSpeed + OurInterceptMis));
                if (currentPos > 0)
                {
                    lblTimetoTarget.Text = "Distance: " + Convert.ToString(CurrentDist);
                }
                else if (currentPos <= 0)
                {
                    lblTimetoTarget.Text = "0";
                }
            }
            else
            {
                if (HitOrMiss == 0)
                {
                    label2.Text = "Hit! Target Destroyed";
                    TmrUpdateDisplay.Enabled = false;
                    MissileInventory--;
                    MisInfo.InventoryTotal1 = MissileInventory;
                    textBox1.Text = Convert.ToString(MissileInventory);
                }
                else
                {
                    label2.Text = "Miss";
                    lblTimetoTarget.Text = "0";
                    TmrUpdateDisplay.Enabled = false;
                    MissileInventory--;
                    MisInfo.InventoryTotal1 = MissileInventory;
                    textBox1.Text = Convert.ToString(MissileInventory);
                }

            }
                   
        }
    }
}
